package cn.com.inossem.adapter;

import java.util.Calendar;
import java.util.List;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import cn.com.inossem.activity.InputActivity;
import cn.com.inossem.activity.NameActivity;
import cn.com.inossem.activity.ProfessionalActivity;
import cn.com.inossem.activity.R;
import cn.com.inossem.db.DatabaseHelper;
import cn.com.inossem.model.ProfessionalInfoBean;
import cn.com.inossem.requestmanager.WooRequestFactory;
import cn.com.inossem.utils.Utils;

public class ProfessionalAdapter extends BaseAdapter {
	private ProfessionalActivity mContext;
	private List<ProfessionalInfoBean> list;
	private List<ProfessionalInfoBean> Xuanzhongmessage;
	private LayoutInflater mInflater;
	public boolean flag = false;
	public int pos = 0;
	int year;
	int month;
	int day;

	public ProfessionalAdapter(ProfessionalActivity context,
			List<ProfessionalInfoBean> list,
			List<ProfessionalInfoBean> Xuanzhongmessage) {
		this.mContext = context;
		this.list = list;
		this.Xuanzhongmessage = Xuanzhongmessage;
		this.mInflater = LayoutInflater.from(context);

		Calendar c = Calendar.getInstance();// c.get(Calendar.YEAR);
		year = c.get(Calendar.YEAR);
		month = c.get(Calendar.MONTH);
		day = c.get(Calendar.DAY_OF_MONTH);
	}

	public void setList(List<ProfessionalInfoBean> list) {
		this.list = list;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public ProfessionalInfoBean getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.professional_item_layout,
					null);
			holder.llayout = (LinearLayout) convertView
					.findViewById(R.id.llayout);
			holder.year = (RelativeLayout) convertView.findViewById(R.id.year);
			holder.name = (TextView) convertView.findViewById(R.id.name);
			holder.time1 = (TextView) convertView.findViewById(R.id.time1);
			holder.time2 = (TextView) convertView.findViewById(R.id.time2);

			holder.textzhiwei = (TextView) convertView
					.findViewById(R.id.textzhiwei);
			holder.addcompany = (TextView) convertView
					.findViewById(R.id.addcompany);
			holder.zhiwei = (RelativeLayout) convertView
					.findViewById(R.id.zhiwei);
			holder.choose = (ImageView) convertView.findViewById(R.id.choose1);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.llayout.setVisibility(View.GONE);
		holder.addcompany.setVisibility(View.GONE);
		int length = list.size();

		if (position < length - 1) {
			holder.llayout.setVisibility(View.VISIBLE);
			holder.name.setText(list.get(position).getComName());
			holder.time1.setText(list.get(position).getStartTime());
			holder.time2.setText(list.get(position).getEndTime());
			// holder.name.setOnClickListener(new OnClickListener() {
			//
			// @Override
			// public void onClick(View v) {
			// // TODO Auto-generated method stub
			// Context.pos = position;
			// Intent intent = new Intent(mContext, InputActivity.class);
			// intent.putExtra("position", position);
			//
			// mContext.startActivityForResult(intent, 0);
			// }
			// });
			// holder.textyear.setText(list.get(position).getPeriod());
			// holder.year.setOnClickListener(new OnClickListener() {
			// @Override
			// public void onClick(View v) {
			// mContext.pos = position;
			// Intent intent = new Intent(mContext, NameActivity.class);
			// intent.putExtra("type", 108);
			// mContext.startActivityForResult(intent, 0);
			// }
			// });
			holder.textzhiwei.setText(list.get(position).getPosition());
			// selectData_info1("t_dictionary", "id="+
			// list.get(position).getPosition()));

			holder.zhiwei.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub

					mContext.pos = position;

					// Intent intent = new Intent(mContext,
					// RetrieveItemActivity.class);
					// intent.putExtra("type", 4);
					// intent.putExtra("itemtype", 4);
					// intent.putExtra("isdanxuan", true);
					// mContext.startActivityForResult(intent, 0);
					Intent intent = new Intent(mContext, NameActivity.class);
					intent.putExtra("type", 110);
					mContext.startActivityForResult(intent, 0);
				}
			});

			holder.time1.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					pos = position;

					DatePickerDialog datePickerDialog = new DatePickerDialog(
							mContext, Datelistener, year, month - 1, day);

					datePickerDialog.show();
				}
			});

			holder.time2.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					pos = position;
					DatePickerDialog datePickerDialog = new DatePickerDialog(
							mContext, Datelistener1, year, month - 1, day);

					datePickerDialog.show();
				}
			});

		} else {
			holder.llayout.setVisibility(View.GONE);
			holder.addcompany.setVisibility(View.VISIBLE);
			holder.addcompany.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {

					Intent intent = new Intent(mContext, InputActivity.class);
					intent.putExtra("position", position);
					mContext.startActivityForResult(intent, 0);
				}
			});
		}

		if (flag && position != list.size() - 1) {
			System.out.println("显示----" + position);
			holder.choose.setVisibility(View.VISIBLE);
		} else {
			System.out.println("隐藏----" + position);
			holder.choose.setVisibility(View.GONE);
		}
		if (getItem(position).getIsselected() == 1) {
			holder.choose.setBackgroundResource(R.drawable.xuanze);
			System.out.println("选中刷新:" + position);
		} else {
			holder.choose.setBackgroundResource(R.drawable.weixuanze);
			System.out.println("未选中刷新:" + position);
		}
		holder.choose.setTag(holder.choose);
		holder.choose.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (getItem(position).getIsselected() == 1) {
					getItem(position).setIsselected(0);
					((ImageView) v.getTag())
							.setBackgroundResource(R.drawable.weixuanze);

					if (Xuanzhongmessage.contains(list.get(position))) {
						System.out.println("取消:" + position);
						Xuanzhongmessage.remove(list.get(position));
					}
				} else {
					getItem(position).setIsselected(1);
					((ImageView) v.getTag())
							.setBackgroundResource(R.drawable.xuanze);
					if (!Xuanzhongmessage.contains(list.get(position))) {
						System.out.println("选中:" + position);
						Xuanzhongmessage.add(list.get(position));
					}
				}
			}
		});

		return convertView;
	}

	private String selectData_info1(String tabname, String selection) {
		String name = "";
		DatabaseHelper dh = new DatabaseHelper(mContext);
		SQLiteDatabase db = dh.getReadableDatabase();
		Cursor cursor = db.query(tabname, null, selection, null, null, null,
				null);
		if (cursor.moveToFirst()) {
			name = cursor.getString(cursor.getColumnIndex("name_cn"));

		}
		cursor.close();
		db.close();
		dh.close();
		return name;
	}

	public void showButton() {
		flag = true;
		this.notifyDataSetChanged();
	}

	public void hiddenButton() {
		flag = false;
		this.notifyDataSetChanged();
	}

	public final class ViewHolder {
		public ImageView choose;
		public LinearLayout llayout;
		public RelativeLayout year;
		public TextView name;
		public TextView textyear;
		public TextView textzhiwei;
		public TextView addcompany;
		public RelativeLayout zhiwei;
		public TextView time1;
		public TextView time2;
	}

	public DatePickerDialog.OnDateSetListener Datelistener = new DatePickerDialog.OnDateSetListener() {
		/**
		 * params：view：该事件关联的组件 params：myyear：当前选择的年 params：monthOfYear：当前选择的月
		 * params：dayOfMonth：当前选择的日
		 */
		@Override
		public void onDateSet(DatePicker view, int myyear, int monthOfYear,
				int dayOfMonth) {

			// 修改year、month、day的变量值，以便以后单击按钮时，DatePickerDialog上显示上一次修改后的值
			// mDateStar.updateDate(myyear, monthOfYear, dayOfMonth);
			// 更新日期
			updateDate(myyear, monthOfYear, dayOfMonth);

		}

		// 当DatePickerDialog关闭时，更新日期显示
		private void updateDate(int myyear, int monthOfYear, int dayOfMonth) {
			// 在TextView上显示日期
			String str = myyear + "." + (monthOfYear + 1) + "." + dayOfMonth;
			try {
				if (!list.get(pos).getEndTime().equals("")
						&& Utils.DateCompare(str, list.get(pos).getEndTime())) {
					Toast.makeText(mContext, "开始日期不能大于结束日期", Toast.LENGTH_LONG)
							.show();
					return;
				}
				list.get(pos).setStartTime(str);
				ProfessionalAdapter.this.notifyDataSetChanged();
				mContext.isShuaxin = false;
				mContext.launchRequest(WooRequestFactory.addProfessional(list
						.get(pos)));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
	};

	private DatePickerDialog.OnDateSetListener Datelistener1 = new DatePickerDialog.OnDateSetListener() {
		/**
		 * params：view：该事件关联的组件 params：myyear：当前选择的年 params：monthOfYear：当前选择的月
		 * params：dayOfMonth：当前选择的日
		 */
		@Override
		public void onDateSet(DatePicker view, int myyear, int monthOfYear,
				int dayOfMonth) {

			// 修改year、month、day的变量值，以便以后单击按钮时，DatePickerDialog上显示上一次修改后的值
			// mDateEnd.updateDate(myyear, monthOfYear, dayOfMonth);
			// 更新日期
			updateDate(myyear, monthOfYear, dayOfMonth);

		}

		// 当DatePickerDialog关闭时，更新日期显示
		private void updateDate(int myyear, int monthOfYear, int dayOfMonth) {
			// 在TextView上显示日期
			String str = myyear + "." + (monthOfYear + 1) + "." + dayOfMonth;
			try {
				if (!list.get(pos).getStartTime().equals("")
						&& Utils.DateCompare(list.get(pos).getStartTime(), str)) {
					Toast.makeText(mContext, "结束日期不能小于开始日期", Toast.LENGTH_LONG)
							.show();
					return;
				}
				list.get(pos).setEndTime(str);
				ProfessionalAdapter.this.notifyDataSetChanged();
				mContext.isShuaxin = false;
				mContext.launchRequest(WooRequestFactory.addProfessional(list
						.get(pos)));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	};
}